<template>
  <div class="execution">
    <basic-container>
      <el-card class="filter-container" shadow="never">
        <div style="margin-top: 10px">
          <el-form :inline="true" size="small" label-width="140px">
            <el-form-item label="操作人">
              <el-input v-model="listQuery.operation"></el-input>
            </el-form-item>
            <el-form-item label="操作时间">
              <el-date-picker
                v-model="dateTimer"
                format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button @click="handelSeach" type="primary" size="small"
                >查询结果</el-button
              >
              <el-button @click="handleResetSearch" size="small">
                重置
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
      <el-card
        class="operate-container"
        shadow="never"
        style="margin-top: 10px"
      >
        <el-button
          class="btn-add"
          icon="el-icon-plus"
          type="primary"
          @click="addProduct()"
          size="mini"
        >
          商品返点设置
        </el-button>
      </el-card>
      <div style="margin-top: 10px">
        <el-table
          :data="list"
          border
          :header-cell-style="{ color: '#4D4D4D', background: '#fafafa' }"
        >
          <el-table-column
            label="商品名称"
            align="center"
            prop="title"
          ></el-table-column>
          <el-table-column label="商品图片" align="center">
            <template slot-scope="{ row }">
              <img :src="row.pic" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column label="价格/规格属性" min-width="120" align="center">
            <template slot-scope="scope">
              <p>商品价格：￥{{ scope.row.skuPrice }}</p>
              <p>规格属性：{{ scope.row.skuValue }}</p>
            </template>
          </el-table-column>
          <el-table-column
            label="一级收益金额"
            align="center"
            prop="levelOne"
          ></el-table-column>
          <el-table-column
            label="二级收益金额"
            align="center"
            prop="levelTwo"
          ></el-table-column>
          <el-table-column
            label="技术收益金额"
            align="center"
            prop="fee"
          ></el-table-column>
          <el-table-column
            label="操作人"
            align="center"
            prop="operationName"
          ></el-table-column>
          <el-table-column
            label="操作时间"
            align="center"
            prop="operationTime"
          ></el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="small"
                @click="handleUpdate(scope.$index, scope.row)"
                type="text"
                icon="el-icon-edit"
                >修改</el-button
              >
              <el-button
                size="small"
                @click="handleDel(scope.$index, scope.row)"
                type="text"
                >删除</el-button
              >
            </template></el-table-column
          >
        </el-table>
      </div>
      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes,prev, pager, next,jumper"
          :page-size="listQuery.size"
          :page-sizes="[5, 10, 15]"
          :current-page.sync="listQuery.current"
          :total="total"
        >
        </el-pagination>
      </div>

      <el-dialog title="修改" :visible.sync="dialogVisible" width="40%">
        <el-form
          :label-position="labelPosition"
          :rules="rules"
          ref="formLabel"
          label-width="150px"
          :model="formLabel"
        >
          <el-form-item label="商品名称：">
            <el-input
              style="width: 203px"
              disabled
              v-model="formLabel.title"
            ></el-input>
          </el-form-item>
          <el-form-item label="一级收益金额：" prop="levelOne">
            <el-input
              style="width: 203px"
              type="number"
              v-model="formLabel.levelOne"
            ></el-input>
          </el-form-item>
          <el-form-item label="二级收益金额：" prop="levelTwo">
            <el-input
              style="width: 203px"
              type="number"
              v-model="formLabel.levelTwo"
            ></el-input>
          </el-form-item>
          <el-form-item label="技术收益金额：" prop="fee">
            <el-input
              style="width: 203px"
              type="number"
              v-model="formLabel.fee"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              @click="handleDetermine('formLabel')"
              type="primary"
              size="small"
            >
              确定
            </el-button>
          </el-form-item>
        </el-form>
      </el-dialog>
    </basic-container>
  </div>
</template>
<script>
import "@/styles/index.scss";
import {
  fetchList,
  updateProductRebates,
  delProductRebates,
} from "@/api/profit/productRebate/index";

const defaultListQuery = {
  current: 1,
  size: 10,
  operation: null,
  startTime: null,
  endTime: null,
};
export default {
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      dateTimer: null,
      list: [],
      total: 0,
      formLabel: {
        id: null,
        levelOne: null,
        levelTwo: null,
        fee: null,
      },
      rules: {
        levelOne: [
          { required: true, message: "请填写一级收益金额", trigger: "blur" },
        ],
        levelTwo: [
          { required: true, message: "请填写二级收益金额", trigger: "blur" },
        ],
        fee: [
          { required: true, message: "请填写技术收益金额", trigger: "blur" },
        ],
      },
      labelPosition: "right",
      dialogVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSizeChange(val) {
      this.listQuery.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.current = val;
      this.getList();
    },
    getList() {
      fetchList(this.listQuery).then((res) => {
        this.list = res.data.data.records;
        this.total = res.data.data.total;
      });
    },
    handelSeach() {
      this.listQuery.startTime = this.dateTimer[0];
      this.listQuery.endTime = this.dateTimer[1];
      this.getList();
    },
    handleResetSearch() {
      this.dateTimer = "";
      this.listQuery = Object.assign({}, this.defaultListQuery);
    },
    handleUpdate(index, row) {
      this.dialogVisible = true;
      this.formLabel = Object.assign({}, row);
    },
    handleDel(index, row) {
      this.$confirm("确定要删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        delProductRebates({ id: row.id }).then((res) => {
          if (res.data.code === 200) {
            this.$message({
              type: "success",
              message: res.data.msg,
            });
            this.getList();
          } else {
            this.$message({
              message: res.data.msg,
              type: "warning",
              duration: 1000,
            });
          }
        });
      });
    },
    handleDetermine(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateProductRebates(this.formLabel).then((res) => {
            this.dialogVisible = false;
            if (res.data.code === 200) {
              this.$message({
                message: res.data.msg,
                type: "success",
                duration: 1000,
              });
              this.getList();
            } else {
              this.$message({
                message: res.data.msg,
                type: "danger",
                duration: 1000,
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addProduct() {
      this.$router.push({
        path: "/profit/productRebate/add",
      });
    },
  },
};
</script>